# -*- coding: utf-8 -*-

import hashlib
from base import UserCase, Runner, TestSuite


class Case(UserCase):

    def test_corp_list(self):
        res = self.c.open('/api/corp/list')
        self.assertEqual(res['code'], 0)

    def test_corp_create(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
            'contacts': self.randhan(3),
            'tel': self.randnum(6),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        id = res['data']['id']

        res = self.c.open('/api/corp/list', {'orderBy': 'corp.id', 'orderDesc': '1'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['itemList'][0]['name'], p['name'])

        res = self.c.open('/api/corp/read', {'id': id})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['name'], p['name'])
        self.assertEqual(res['data']['contacts'], p['contacts'])
        self.assertEqual(res['data']['tel'], p['tel'])


    def test_corp_create_login(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

    def test_corp_delete(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/corp/list', {'orderBy': 'corp.id', 'orderDesc': '1'})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['itemList'][0]['name'], p['name'])

        res = self.c.open('/api/corp/delete', {'id': id})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/corp/list')
        self.assertEqual(res['code'], 0)
        self.assertNotEqual(res['data']['itemList'][0]['id'], id)


    def test_corp_update(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/corp/read', {'id': id})
        self.assertEqual(res['data']['name'], p['name'])

        p = {
            'name': self.randhan(8),
            'contacts': self.randhan(3),
            'tel': self.randnum(6),
            'id': id,
        }
        res = self.c.open('/api/corp/update', p)
        self.assertEqual(res['code'], 0)
        res = self.c.open('/api/corp/read', {'id': id})
        self.assertEqual(res['data']['name'], p['name'])
        self.assertEqual(res['data']['contacts'], p['contacts'])
        self.assertEqual(res['data']['tel'], p['tel'])


    def test_corp_option(self):
        res = self.c.open('/api/corp/option')
        self.assertEqual(res['code'], 0)

        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/corp/option')
        self.assertEqual(res['code'], 0)


    def test_corp_option_filter(self):
        p = {
            'name': self.randhan(16),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        pp = {
            'name': self.randhan(16),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', pp)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/corp/option', {'q': p['name']})
        self.assertEqual(res['data']['itemList'][0]['value'], id)
        self.assertEqual(res['data']['itemList'][0]['name'], p['name'])


    def test_corp_delete_corp_user(self):
        p = {
            'name': self.randhan(16),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        res = self.c.open('/api/corp-user/list', {'corp': id})
        self.assertEqual(res['data']['count'], 1)

        res = self.c.open('/api/corp/delete', {'id': id})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/corp-user/list', {'corp': id})
        self.assertNotEqual(res['code'], 0)


    def test_corp_delete_corp_device(self):
        p = {
            'name': self.randhan(16),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        corp = res['data']['id']

        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp,
        }
        res = self.c.open('/api/device/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/corp-device/list', {'corp': corp})
        self.assertEqual(res['data']['count'], 1)

        res = self.c.open('/api/corp/delete', {'id': corp})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/corp-device/list', {'corp': corp})
        self.assertEqual(res['code'], 0)


test_list = [
    'test_corp_create',
    'test_corp_list',
    'test_corp_delete',
    'test_corp_update',
    'test_corp_create_login',
    'test_corp_option',
    'test_corp_option_filter',
    'test_corp_delete_corp_user',
    'test_corp_delete_corp_device',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



